package LL5;

public class Main {


    // Definition for singly-linked list.
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    static class Solution {
        public int getDecimalValue(ListNode head) {
            if (head == null) {
                return 0;
            }
            // 1. 定义操作节点指针变量
            int res = head.val; //赋值为第1个节点的值
            ListNode cur = head.next; //指向第2个节点

            //2.移动操作节点指针变量，计算二进制的值
            while (cur != null) {
                res = res * 2 + cur.val; //右侧每增加一个数，需对原来的数乘2再加上新对应的数
                cur = cur.next;  //移动指针变量
            }
            return res;
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(0);
        head.next = new ListNode(0);
//        head.next.next = new ListNode(1);

        Solution solution = new Solution();
        int value = solution.getDecimalValue(head);
        System.out.println(value);
    }
}
